package net.xrkw.server.module.client.service.softinfo;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import net.xrkw.server.module.client.controller.admin.softinfo.vo.*;
import net.xrkw.server.module.client.dal.dataobject.softinfo.SoftInfoDO;
import net.xrkw.server.framework.common.pojo.PageResult;
import net.xrkw.server.framework.common.pojo.PageParam;
import net.xrkw.server.framework.common.util.object.BeanUtils;

import net.xrkw.server.module.client.dal.mysql.softinfo.SoftInfoMapper;

import static net.xrkw.server.framework.common.exception.util.ServiceExceptionUtil.exception;
import static net.xrkw.server.framework.common.util.collection.CollectionUtils.convertList;
import static net.xrkw.server.framework.common.util.collection.CollectionUtils.diffList;
import static net.xrkw.server.module.client.enums.ErrorCodeConstants.*;

/**
 * 软件信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SoftInfoServiceImpl implements SoftInfoService {

    @Resource
    private SoftInfoMapper softInfoMapper;

    @Override
    public Long createSoftInfo(SoftInfoSaveReqVO createReqVO) {
        // 插入
        SoftInfoDO softInfo = BeanUtils.toBean(createReqVO, SoftInfoDO.class);
        softInfoMapper.insert(softInfo);

        // 返回
        return softInfo.getId();
    }

    @Override
    public void updateSoftInfo(SoftInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateSoftInfoExists(updateReqVO.getId());
        // 更新
        SoftInfoDO updateObj = BeanUtils.toBean(updateReqVO, SoftInfoDO.class);
        softInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteSoftInfo(Long id) {
        // 校验存在
        validateSoftInfoExists(id);
        // 删除
        softInfoMapper.deleteById(id);
    }

    @Override
        public void deleteSoftInfoListByIds(List<Long> ids) {
        // 删除
        softInfoMapper.deleteByIds(ids);
        }


    private void validateSoftInfoExists(Long id) {
        if (softInfoMapper.selectById(id) == null) {
            throw exception(SOFT_INFO_NOT_EXISTS);
        }
    }

    @Override
    public SoftInfoDO getSoftInfo(Long id) {
        return softInfoMapper.selectById(id);
    }

    @Override
    public PageResult<SoftInfoDO> getSoftInfoPage(SoftInfoPageReqVO pageReqVO) {
        return softInfoMapper.selectPage(pageReqVO);
    }

}